import {Injectable} from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import {Observable} from 'rxjs';
import {ApiService} from '../../utils/api.service';

@Injectable({
  providedIn: 'root'
})
export class AuthAccessGuard  {

  constructor(private api: ApiService, private router: Router) {
  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return new Promise((resolve, reject) => {
      this.api.heatBeat().subscribe(res => {
        resolve(true);
      }, error => {
        resolve(false);
        this.router.navigate(['login']).then();
      });
    });
  }
}

